package com.hibiscusking.org.controller;

import com.hibiscusking.basic.anno.PreAuthorize;
import com.hibiscusking.basic.util.AjaxResult;
import com.hibiscusking.basic.util.PageList;
import com.hibiscusking.org.domain.Department;
import com.hibiscusking.org.query.DepartmentQuery;
import com.hibiscusking.org.service.DeparmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@PreAuthorize
public class DepartmentController {
    @Autowired
    private DeparmentService deparmentService;

    @PutMapping
    @PreAuthorize(name = "部门添加或者修改",sn = "department:add")
    public AjaxResult add(@RequestBody Department department) {
        System.out.println(department);
        if (department.getId() == null) {
            deparmentService.add(department);
        } else {
            deparmentService.update(department);
        }
        return AjaxResult.me().success();
    }
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "部门删除",sn = "department:dele")
    public AjaxResult dele(@PathVariable("id") Long id) {
        deparmentService.dele(id);
        return AjaxResult.me().success();
    }
    @GetMapping
    @PreAuthorize(name = "部门查询全部",sn = "department:findAll")
    public List<Department> findAll() {
        List<Department> all = deparmentService.findAll();
        return all;
    }
    /** 根据id查询一个 */
    @GetMapping("/{id}")
    @PreAuthorize(name = "根据id查询单个部门",sn = "department:findById")
    public Department findById(@PathVariable("id") Long id){
        return deparmentService.findById(id);
    }

    /** 分页查询 */
    @PostMapping
    @PreAuthorize(name = "分页查询部门",sn = "department:queryByPage")
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query){
        return deparmentService.queryByPage(query);
    }
    @PatchMapping
    @PreAuthorize(name = "批量删除部门",sn = "department:batchDele")
    public AjaxResult batchDele(@RequestBody List<Long> ids){
        deparmentService.batchDele(ids);
        return AjaxResult.me().success();
    }
    @PostMapping("/tree")
    @PreAuthorize(name = "部门数",sn = "department:batchDele")
    public List<Department> departTree(){
        return deparmentService.departTree();
    }
}
